package com.cskaoyan.service;

import com.cskaoyan.bean.CskaoyanmallAd;
import com.cskaoyan.bean.CskaoyanmallAdExample;
import com.cskaoyan.mapper.CskaoyanmallAdMapper;
import com.cskaoyan.vo.AdVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CskaoyanmallAdServiceImpl implements CskaoyanmallAdService{

    @Resource
    private CskaoyanmallAdMapper cskaoyanmallAdMapper;

    @Override
    public long countByExample(CskaoyanmallAdExample example) {
        return cskaoyanmallAdMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(CskaoyanmallAdExample example) {
        return cskaoyanmallAdMapper.deleteByExample(example);
    }

    @Override
    public int insert(CskaoyanmallAd record) {
        return cskaoyanmallAdMapper.insert(record);
    }

    @Override
    public int insertSelective(CskaoyanmallAd record) {
        return cskaoyanmallAdMapper.insertSelective(record);
    }

    @Override
    public List<CskaoyanmallAd> selectByExample(CskaoyanmallAdExample example) {
        return cskaoyanmallAdMapper.selectByExample(example);
    }

    @Override
    public CskaoyanmallAd selectByPrimaryKey(Integer id) {
        return cskaoyanmallAdMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(CskaoyanmallAd record, CskaoyanmallAdExample example) {
        return cskaoyanmallAdMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(CskaoyanmallAd record, CskaoyanmallAdExample example) {
        return cskaoyanmallAdMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(CskaoyanmallAd record) {
        return cskaoyanmallAdMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CskaoyanmallAd record) {
        return cskaoyanmallAdMapper.updateByPrimaryKey(record);
    }

    @Override
    public AdVo query(Integer page, Integer limit, String name, String content, String sort, String order) {
        //分页和排序
        //页码、数据量
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        CskaoyanmallAdExample adExample = new CskaoyanmallAdExample();
        adExample.setOrderByClause(sort + " " + order);//${}做Sql拼接
        CskaoyanmallAdExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null && !"".equals(name)){
            criteria.andNameLike("%" + name +"%");
        }
        if (content !=null && !"".equals(content)){
            criteria.andContentLike("%"+content +"%");
        }
        List<CskaoyanmallAd> ads = cskaoyanmallAdMapper.selectByExample(adExample);
        //total !=comment.size();
        //total是满足条件的所有的数据量 👉 而不是当前的limit
        //可以通过PageInfo获得分页信息
        PageInfo<CskaoyanmallAd> userPageInfo = new PageInfo<>(ads);//查询结果放进来
        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample

        AdVo adVo = new AdVo();
        adVo.setItems(ads);
        adVo.setTotal(total);
        return adVo;
    }

    @Override
    public int delete(CskaoyanmallAd cskaoyanmallAd) {
        cskaoyanmallAd.setDeleted(true);
        int i = updateByPrimaryKey(cskaoyanmallAd);
        if (i == 1) {
            return 0;
        }else {
            return 400;
        }
    }
}
